﻿///<reference path="_moduleReferences.ts" />

module ScrambledEggs.Modules {
    export class EndGame implements IModule {
        private _endbox: JQuery;

        constructor() {
            this._endbox = $("#endbox").remove();
        }

        initialize(gameState: GameStateProvider, onUpdate: () => void ) {
            var $doc = $(document);
            $doc.on("click", "button.getnotified", function () {
                var button = this;
                var email = $(".notifyEmail").val();
                $.ajax({
                    url: "/Api/Notifications",
                    method: "PUT",
                    data: { SaveKey: gameState.Key, Email: email }
                }).done(function () {
                    $("<p><strong><br />Thank you!<br />&nbsp;</strong></p>").insertAfter($(button).parent());
                    $(button).parent().remove();
                });
            });
            $doc.on("click", "button.startanew", function () {
                window.location.href = "/";
            });
        }

        update(gameState: GameStateProvider, triggerSource: IModule): void {
            var me = this;
            var endGameReached = gameState.State.PartTimeJob != PartTimeJob.None
                && gameState.State.PartTimeJob != PartTimeJob.Shoemaker
                && !gameState.Settings.IgnoreEarlyEnding;
            if (endGameReached) {
                $("button.save").first().click();
                Utility.General.OptionBox(
                    me._endbox.html()
                )
            }
        }
    }
}